package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.ExceptionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionExceptionDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;

import cn.edu.scau.cmi.hombio.domain.ExceptionType;
import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.InspectionException;
import cn.edu.scau.cmi.hombio.domain.Pigsty;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for InspectionException entities
 * 
 */

@Service("InspectionExceptionService")

@Transactional
public class InspectionExceptionServiceImpl implements InspectionExceptionService {

	/**
	 * DAO injected by Spring that manages ExceptionType entities
	 * 
	 */
	@Autowired
	private ExceptionTypeDAO exceptionTypeDAO;

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionDAO inspectionExceptionDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * Instantiates a new InspectionExceptionServiceImpl.
	 *
	 */
	public InspectionExceptionServiceImpl() {
	}

	/**
	 */
	@Transactional
	public InspectionException findInspectionExceptionByPrimaryKey(Integer id) {
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(id);
	}

	/**
	 * Delete an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public InspectionException deleteInspectionExceptionExceptionType(Integer inspectionexception_id, Integer related_exceptiontype_id) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id, -1, -1);
		ExceptionType related_exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype_id, -1, -1);

		inspectionexception.setExceptionType(null);
		related_exceptiontype.getInspectionExceptions().remove(inspectionexception);
		inspectionexception = inspectionExceptionDAO.store(inspectionexception);
		inspectionExceptionDAO.flush();

		related_exceptiontype = exceptionTypeDAO.store(related_exceptiontype);
		exceptionTypeDAO.flush();

		exceptionTypeDAO.remove(related_exceptiontype);
		exceptionTypeDAO.flush();

		return inspectionexception;
	}

	/**
	 * Save an existing Inspection entity
	 * 
	 */
	@Transactional
	public InspectionException saveInspectionExceptionInspection(Integer id, Inspection related_inspection) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(id, -1, -1);
		Inspection existinginspection = inspectionDAO.findInspectionByPrimaryKey(related_inspection.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginspection != null) {
			existinginspection.setId(related_inspection.getId());
			existinginspection.setTime(related_inspection.getTime());
			existinginspection.setMemo(related_inspection.getMemo());
			related_inspection = existinginspection;
		}

		inspectionexception.setInspection(related_inspection);
		related_inspection.getInspectionExceptions().add(inspectionexception);
		inspectionexception = inspectionExceptionDAO.store(inspectionexception);
		inspectionExceptionDAO.flush();

		related_inspection = inspectionDAO.store(related_inspection);
		inspectionDAO.flush();

		return inspectionexception;
	}

	/**
	 * Save an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public InspectionException saveInspectionExceptionExceptionType(Integer id, ExceptionType related_exceptiontype) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(id, -1, -1);
		ExceptionType existingexceptionType = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexceptionType != null) {
			existingexceptionType.setId(related_exceptiontype.getId());
			existingexceptionType.setName(related_exceptiontype.getName());
			existingexceptionType.setMemo(related_exceptiontype.getMemo());
			related_exceptiontype = existingexceptionType;
		}

		inspectionexception.setExceptionType(related_exceptiontype);
		related_exceptiontype.getInspectionExceptions().add(inspectionexception);
		inspectionexception = inspectionExceptionDAO.store(inspectionexception);
		inspectionExceptionDAO.flush();

		related_exceptiontype = exceptionTypeDAO.store(related_exceptiontype);
		exceptionTypeDAO.flush();

		return inspectionexception;
	}

	/**
	 * Delete an existing Pigsty entity
	 * 
	 */
	@Transactional
	public InspectionException deleteInspectionExceptionPigsty(Integer inspectionexception_id, Integer related_pigsty_id) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id, -1, -1);
		Pigsty related_pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		inspectionexception.setPigsty(null);
		related_pigsty.getInspectionExceptions().remove(inspectionexception);
		inspectionexception = inspectionExceptionDAO.store(inspectionexception);
		inspectionExceptionDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		pigstyDAO.remove(related_pigsty);
		pigstyDAO.flush();

		return inspectionexception;
	}

	/**
	 * Save an existing Pigsty entity
	 * 
	 */
	@Transactional
	public InspectionException saveInspectionExceptionPigsty(Integer id, Pigsty related_pigsty) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(id, -1, -1);
		Pigsty existingpigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsty != null) {
			existingpigsty.setId(related_pigsty.getId());
			existingpigsty.setNumber(related_pigsty.getNumber());
			existingpigsty.setRfid(related_pigsty.getRfid());
			existingpigsty.setCapacity(related_pigsty.getCapacity());
			existingpigsty.setMemo(related_pigsty.getMemo());
			related_pigsty = existingpigsty;
		}

		inspectionexception.setPigsty(related_pigsty);
		related_pigsty.getInspectionExceptions().add(inspectionexception);
		inspectionexception = inspectionExceptionDAO.store(inspectionexception);
		inspectionExceptionDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		return inspectionexception;
	}

	/**
	 * Load an existing InspectionException entity
	 * 
	 */
	@Transactional
	public Set<InspectionException> loadInspectionExceptions() {
		return inspectionExceptionDAO.findAllInspectionExceptions();
	}

	/**
	 * Delete an existing InspectionException entity
	 * 
	 */
	@Transactional
	public void deleteInspectionException(InspectionException inspectionexception) {
		inspectionExceptionDAO.remove(inspectionexception);
		inspectionExceptionDAO.flush();
	}

	/**
	 * Delete an existing Inspection entity
	 * 
	 */
	@Transactional
	public InspectionException deleteInspectionExceptionInspection(Integer inspectionexception_id, Integer related_inspection_id) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id, -1, -1);
		Inspection related_inspection = inspectionDAO.findInspectionByPrimaryKey(related_inspection_id, -1, -1);

		inspectionexception.setInspection(null);
		related_inspection.getInspectionExceptions().remove(inspectionexception);
		inspectionexception = inspectionExceptionDAO.store(inspectionexception);
		inspectionExceptionDAO.flush();

		related_inspection = inspectionDAO.store(related_inspection);
		inspectionDAO.flush();

		inspectionDAO.remove(related_inspection);
		inspectionDAO.flush();

		return inspectionexception;
	}

	/**
	 * Return a count of all InspectionException entity
	 * 
	 */
	@Transactional
	public Integer countInspectionExceptions() {
		return ((Long) inspectionExceptionDAO.createQuerySingleResult("select count(o) from InspectionException o").getSingleResult()).intValue();
	}

	/**
	 * Return all InspectionException entity
	 * 
	 */
	@Transactional
	public List<InspectionException> findAllInspectionExceptions(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<InspectionException>(inspectionExceptionDAO.findAllInspectionExceptions(startResult, maxRows));
	}

	/**
	 * Save an existing InspectionException entity
	 * 
	 */
	@Transactional
	public void saveInspectionException(InspectionException inspectionexception) {
		InspectionException existingInspectionException = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception.getId());

		if (existingInspectionException != null) {
			if (existingInspectionException != inspectionexception) {
				existingInspectionException.setId(inspectionexception.getId());
				existingInspectionException.setMemo(inspectionexception.getMemo());
			}
			inspectionexception = inspectionExceptionDAO.store(existingInspectionException);
		} else {
			inspectionexception = inspectionExceptionDAO.store(inspectionexception);
		}
		inspectionExceptionDAO.flush();
	}
}
